package cn.wolfcode.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.ChoiceMapper;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/activityItem")
public class ActivityItemController {

    @Autowired
    private IActivityItemService activityItemService;
    @Autowired
    private ChoiceMapper choiceMapper;
    @Autowired
    private IActivityService activityService;
    @RequiredPermission(name="投票明细显示",expression ="activityItem:list")
    @RequestMapping("/list")
    public String list(Model model,Long id){
        Long empId = UserContext.getCurrentUser().getId();
        List<Choice> choices = choiceMapper.selectByempIdAndActivityId(empId, id);
        if (choices != null && choices.size() > 0){
            throw new RuntimeException("已经投过票了");
        }
        List<ActivityItem> items = activityItemService.selectByActivityId(id);
        Activity activity = activityService.get(id);
        model.addAttribute("activity", activity);
        model.addAttribute("items", items);
        model.addAttribute("activityId", id);

        return "activity/page";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name="投票明细删除",expression ="activityItem:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            activityItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="投票明细保存",expression ="activityItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Long id,Long activityId){
       if (id != null){
           activityItemService.save(id,activityId);
       }
       return new JsonResult();
    }
    @RequestMapping("/listResult")
    @RequiredPermission(name="投票明细保存",expression ="activityItem:listResult")
    public String listResult(Long activityId,Model model){
        Activity activity = activityService.get(activityId);
        List<ActivityItem> activityItems = activityItemService.selectByActivityId(activityId);
        //创建集合收集数据
        int count = 0;
        for (ActivityItem activityItem : activityItems) {
            count += activityItem.getNum();
        }
        model.addAttribute("activityItems", activityItems);
        model.addAttribute("count", count);
        model.addAttribute("activity", activity);
        return "activity/result";
    }
}
